﻿using System.Collections.Generic;
using System.Linq;

namespace CSW.Framework.Common.Utils.StringUtils
{
    /// <summary>
    /// StringUtils static class.
    /// </summary>
    public static class StringUtils
    {
        /// <summary>
        /// Gets the trailing digits of a string.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>The trailing digits of a string.</returns>
        public static string GetTrailingDigits(IEnumerable<char> input)
        {
            if (input == null)
                return null;

            char[] output = GetLeadingDigits(input.Reverse()).Reverse().ToArray();
            return new string(output);
        }

        /// <summary>
        /// Gets the leading digits of a string.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>The leading digits of a string.</returns>
        public static string GetLeadingDigits(IEnumerable<char> input)
        {
            if (input == null)
                return null;

            string output = string.Empty;
            foreach (char c in input)
            {
                if (!char.IsNumber(c))
                    break;

                output += c;
            }

            return output;
        }

        /// <summary>
        /// Gets the trailing letters or digits of a string.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>The trailing letters or digits of a string.</returns>
        public static string GetTrailingLettersOrDigits(IEnumerable<char> input)
        {
            if (input == null)
                return null;

            char[] output = GetLeadingLettersOrDigits(input.Reverse()).Reverse().ToArray();
            return new string(output);
        }

        /// <summary>
        /// Gets the leading letters or digits of a string.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>The leading letters or digits of a string.</returns>
        public static string GetLeadingLettersOrDigits(IEnumerable<char> input)
        {
            if (input == null)
                return null;

            string output = string.Empty;
            foreach (char c in input)
            {
                if (!char.IsLetterOrDigit(c))
                    break;

                output += c;
            }

            return output;
        }

        /// <summary>
        /// Determines whether the specified input string is all letters.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns><c>true</c> if the specified input string is all letters; otherwise, <c>false</c>.</returns>
        public static bool IsAllLetters(IEnumerable<char> input)
        {
            if (input == null || !input.Any())
                return false;

            foreach (char c in input)
            {
                if (!char.IsLetter(c))
                    return false;
            }

            return true;
        }
    }
}
